import { Attribute } from "../etc/Attribute";
import { util } from "../etc/Util";
import { CDTimer } from "../gameEtc/CDTimer";
import { LevelModuleC } from "../module/LevelModule";
import { PlayerModuleC, PlayerModuleData } from "../module/PlayerModule";
import MainUI_Generate from "../ui-generate/MainUI_generate";
import { GrowWindow } from "./GrowWindow";

export class MainUI extends MainUI_Generate{
    private pressed:boolean = false
    private forceValue = 0

    private resetCD:CDTimer = new CDTimer(1e4)

    private playerMD:PlayerModuleC
    private playerData:PlayerModuleData
    private levelMD:LevelModuleC

    onStart(){
        this.playerMD = ModuleService.getModule(PlayerModuleC)
        this.levelMD = ModuleService.getModule(LevelModuleC)
        this.playerData = DataCenterC.getData(PlayerModuleData)

        this.playerData.onDataChange.add(this.onPlayerDataChanged, this)
        
        this.btn_reset_position.onPressed.add(this.onBtnResitPositionClicked.bind(this))
        this.btn_grow.onPressed.add(()=>{
            UIService.show(GrowWindow)
        })
        this.btn_reset_game.onPressed.add(()=>{
            this.levelMD.requestResetGame()
        })
        this.btn_jump.onPressed.add(this.onJumpClicked.bind(this))

        this.onPlayerDataChanged()

        this.canUpdate = true
    }

    onShow(){
        this.resetCDUpdate()
    }

    onUpdate(){

        this.resetCDUpdate()
    }

    private onPlayerDataChanged(){
        this.txt_money.text = `红包x${this.playerMD.getAttr(Attribute.EnumAttributeType.money)}`
    }

    private resetCDUpdate(){
        if(this.resetCD.getLeftTime() <= 0) {
            this.btn_reset_position_mask.fanShapedValue = 1
            return
        }
        this.btn_reset_position_mask.fanShapedValue = 1 - this.resetCD.getLeftTime() / this.resetCD.getCD()
    }

    onJumpClicked() {
        Player.localPlayer.character.jump()
    }

    onBtnResitPositionClicked() {
        if(!this.resetCD.canUse()) return
        this.levelMD.resetPlayerPositionToCurrentCheckpointStartPosition()
        this.playerMD.clearPlayerDirectionalImpulse()
    }
    
}